package top.scsoul.voes.examination.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import top.scsoul.voes.common.constance.ExamRegistrationState;
import top.scsoul.voes.common.exception.SystemException;
import top.scsoul.voes.examination.entity.ExamRegistration;
import top.scsoul.voes.examination.entity.Examination;
import top.scsoul.voes.examination.mapper.ExamRegistrationMapper;
import top.scsoul.voes.examination.mapper.ExaminationMapper;
import top.scsoul.voes.examination.service.ExamRegistrationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ExamRegistration)表服务实现类
 *
 * @author guolei
 * @since 2021-07-25 09:47:56
 */
@Service("examRegistrationService")
public class ExamRegistrationServiceImpl implements ExamRegistrationService {
    @Resource
    private ExamRegistrationMapper examRegistrationMapper;
    @Autowired
    ExaminationMapper examinationMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ExamRegistration queryById(Integer id) {
        return this.examRegistrationMapper.queryById(id);
    }

    @Override
    public List<String> queryExamCardNumByExamId(Integer examId) {

        return examRegistrationMapper.queryExamCardNumByExamId(examId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ExamRegistration> queryAllByLimit(int offset, int limit) {
        return this.examRegistrationMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param examRegistration 实例对象
     * @return 实例对象
     */
    @Override
    public ExamRegistration insert(ExamRegistration examRegistration) {
        //设置状态表示成功报名，等待考试
        examRegistration.setState(ExamRegistrationState.WAIT);
        Integer examId = examRegistration.getExamId();
        Examination examination = examinationMapper.queryById(examId);
        if (examination == null) {
            throw new SystemException("无该场考试信息");
        }
        String username = examRegistration.getUsername();
        Integer integer = examRegistrationMapper.queryByExamIdAndUsername(examId, username);
        if (integer > 0) {
            throw new SystemException("已提交过报名信息");
        }

        //更新人数
        Integer examJoinNum = examination.getExamJoinNum();
        Integer count = examJoinNum + 1;
        examination.setExamJoinNum(count);
        examinationMapper.update(examination);
        //座位号  后面用
        examRegistration.setSeatNum(String.valueOf(count));
        //准考证号码
        Snowflake snowflake = IdUtil.getSnowflake();
        examRegistration.setExamCardNum(String.valueOf(snowflake.nextId()));

        //插入数据
        examRegistrationMapper.insert(examRegistration);
        return examRegistration;
    }

    /**
     * 修改数据
     *
     * @param examRegistration 实例对象
     * @return 实例对象
     */
    @Override
    public ExamRegistration update(ExamRegistration examRegistration) {
        this.examRegistrationMapper.update(examRegistration);
        return this.queryById(examRegistration.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.examRegistrationMapper.deleteById(id) > 0;
    }

    @Override
    public PageInfo<ExamRegistration> queryAll(ExamRegistration examRegistration, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<ExamRegistration> examRegistrations = examRegistrationMapper.queryAll(examRegistration);
        return new PageInfo<ExamRegistration>(examRegistrations);
    }

    @Override
    public List<ExamRegistration> queryAll(ExamRegistration examRegistration) {
        return examRegistrationMapper.queryAll(examRegistration);
    }

    @Override
    public ExamRegistration queryExamRegistrationByUsernameAndExamId(String username, Integer examId) {

        return examRegistrationMapper.queryExamRegistrationByUsernameAndExamId(username, examId);
    }

}